<?php
require_once dirname(__FILE__)."/../init.php";

$smsId = $_POST['smsId'];
$sourcePage = $_POST['sourcePage'];

if ($sourcePage == "drafts") {

    $sms = ServiceFactory::getSMSService()->getById($smsId);
    ServiceFactory::getSMSService()->send($sms);

    $sms->setStatus(SMS::STATUS_SENT);
    ServiceFactory::getSMSService()->update($sms);

    ?>

    <fb:redirect url="index.php?_page=drafts&operation=send&success=true" />

    <?php

} else if ($sourcePage == "compose") {

    $recipient = $_POST['recipient'];
    $message = $_POST['message'];

    $send = $_POST['send'];
    $draft = $_POST['draft'];

    $sms = new SMS();
    $sms->setMessage($message);
    $sms->setRecipientName(RecipientUtils::extractRecipientName($recipient));
    $recipientName = $sms->getRecipientName();
    $sms->setRecipientMobileNumber(RecipientUtils::extractRecipientNumber($recipient));
    $sms->setLastModifiedDate(date("Y-m-d H:i:s"));


    $owner = ServiceFactory::getUserService()->getByUid($_uid);
    $sms->setSenderId($owner->getId());

    if ($send == null) {
    // send as draft and redirect to compose message
    $sms->setStatus(SMS::STATUS_DRAFT);
    ServiceFactory::getSMSService()->add($sms);
    ?>

    <fb:redirect url="index.php?_page=drafts&operation=add&success=true" />

    <?php
    } else {
        // send now and save
        ServiceFactory::getSMSService()->send($sms);
        
        $sms->setStatus(SMS::STATUS_SENT);
        ServiceFactory::getSMSService()->add($sms);
    ?>

    <fb:redirect url="index.php?_page=compose&operation=add&success=true" />

    <?php
    }

}
?>
